package com.leyou.item.service.impl;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.constants.RedisConstants;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Service
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Brand> findBrandByCategoryId(Long cid) {
        return baseMapper.findBrandByCategoryId(cid);
    }

    @Override
    public PageDTO findBrandBypage(Integer page, Integer rows, String key) {
        //页码不能超过100，每页大小不能小于5
        page = Math.min(page, 100);
        rows = Math.max(rows,5);
        Page<Brand> pages = new Page(page, rows);
        // 3.判断key是否存在
        boolean isKeyExists = StringUtils.isNoneBlank(key);
        // 4.如果key存在，添加like和eq的查询条件，否则不添加
        query().like(isKeyExists, "name", key)
                .or()
                .eq(isKeyExists, "letter", key)
                .page(pages);
        // 5.封装结果
        List<Brand> list = pages.getRecords();
        return new PageDTO<>(pages.getTotal(), pages.getPages(), BrandDTO.convertEntityList(list));
    }

    @Override
    public void saveBrand(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        //先向brand表中插入
        save(brand);
        //向中间表中批量插入
        List<Long> categoryIds = brandDTO.getCategoryIds();
        Long bid = brand.getId();
        for (Long cid : categoryIds) {
            baseMapper.saveCategoryBrand(cid,bid);
        }
        String imgName = brandDTO.getImage().split("aliyuncs.com/")[1];
        redisTemplate.delete(RedisConstants.IMAGE_NAME+imgName);
        redisTemplate.opsForSet().remove(RedisConstants.IMAGE_NAMES,imgName);
    }

    @Override
    public void updateBrand(BrandDTO brandDTO) {
        //先更新brand
        Brand brand = brandDTO.toEntity(Brand.class);
        updateById(brand);
        //中间表先删后加
        baseMapper.deleteByBid(brand.getId());
        //向中间表中批量插入
        List<Long> categoryIds = brandDTO.getCategoryIds();
        Long bid = brand.getId();
        for (Long cid : categoryIds) {
            baseMapper.saveCategoryBrand(cid,bid);
        }
        String imgName = brandDTO.getImage().split("aliyuncs.com/")[1];
        redisTemplate.delete(RedisConstants.IMAGE_NAME+imgName);
        redisTemplate.opsForSet().remove(RedisConstants.IMAGE_NAMES,imgName);
    }

    @Override
    public void deleteBrandById(Long id) {
        removeById(id);
        baseMapper.deleteByBid(id);
    }

    @Override
    public void saveImgName(String imgName) {
        redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().set(RedisConstants.IMAGE_NAME+imgName,imgName, 1,TimeUnit.HOURS);
        redisTemplate.opsForSet().add(RedisConstants.IMAGE_NAMES,imgName);
    }
}
